package com.javaEE.gamepigeon.service.impl;

import com.javaEE.gamepigeon.dao.CompetitionDao;
import com.javaEE.gamepigeon.dao.DateDetailDao;
import com.javaEE.gamepigeon.entity.DateDetail;
import com.javaEE.gamepigeon.service.DateDetailService;
import com.javaEE.gamepigeon.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


@Service
public class DateDetailServiceImpl implements DateDetailService {

    @Autowired
    DateDetailDao dateDetailDao;

    @Autowired
    CompetitionDao competitionDao;

    @Override
    @Transactional
    public Result addDetail(DateDetail[] list, Integer comId) {
        if(dateDetailDao.addDetail(list,comId)==list.length){
            competitionDao.updateCompetition(comId,null,null,null,null,null,null,null,null, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }

    @Transactional
    @Override
    public Result deleteDetail(Integer[] list, Integer comId) {
        if(dateDetailDao.deleteDetail(list) == list.length){
            competitionDao.updateCompetition(comId,null,null,null,null,null,null,null,null, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
            return Result.success("删除成功！");
        }else {
            return Result.error("删除失败！");
        }
    }

    @Override
    public List<DateDetail> queryDetail(Integer comId) {
        return competitionDao.queryDetail(comId);
    }
}
